package pl.elka.simulator.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import pl.elka.simulator.core.mobilityManager.MobilityManagerType;
import pl.elka.simulator.mediator.ActionType;
import pl.elka.simulator.mediator.ActionValue;
import pl.elka.simulator.mediator.SimulationFasade;

public class AdministrationPanel extends JPanel{

	final JComboBox<MobilityManagerType> mobilityManagersCB;
	final JButton addMobilityManagerB;
	final JList<String> addedMobilityManagersL;
	final DefaultListModel<String> addedMobilityManagersLM;
	final JTextField mobilityManagerIdTF;
	final ConfigurationPanel configurationPanel;
	
	public AdministrationPanel(){
		
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
	
		
		mobilityManagersCB = new JComboBox<MobilityManagerType>(MobilityManagerType.values());				
		addMobilityManagerB = new JButton("Add");
		addedMobilityManagersLM = new DefaultListModel<String>();
		addedMobilityManagersL = new JList<String>(addedMobilityManagersLM);
		mobilityManagerIdTF = new JTextField();
		configurationPanel = new ConfigurationPanel();
		
		addMobilityManagerB.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MobilityManagerType type = (MobilityManagerType)mobilityManagersCB.getSelectedItem();
				ActionValue value = new ActionValue(new Object[]{type,mobilityManagerIdTF.getText()});
				SimulationFasade.getInstance().handleAction(ActionType.AddMobilityManager,value);
				addedMobilityManagersLM.addElement(mobilityManagerIdTF.getText());				
			}
		});
		
		addedMobilityManagersL.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				configurationPanel.regenerateContent(addedMobilityManagersL.getSelectedValue());				
			}
		});
		
		add(mobilityManagersCB);
		add(mobilityManagerIdTF);
		add(addMobilityManagerB);
		add(addedMobilityManagersL);
		add(configurationPanel);
		//add(new JPanel());
		
	}
}
